package org.example.platform.beans.factory.Autoware.support;


import org.example.platform.beans.factory.Autoware.BeanFactory;
import org.example.platform.beans.factory.Autoware.config.BeanDefinition;

/**
 *  抽象的 Bean 工厂基类，定义模板方法
 */
public  abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {

    @Override
    public Object getBean(String name) throws Exception {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) throws Exception {
        return doGetBean(name, args);
    }

    protected <T> T doGetBean(final String name, final Object[] args) throws Exception {
        Object bean = getSingleton(name);
        if (bean != null) {
            return (T) bean;
        }

        BeanDefinition beanDefinition = getBeanDefinition(name);
        return (T) createBean(name, beanDefinition, args);
    }

    protected abstract BeanDefinition getBeanDefinition(String beanName) throws Exception;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws Exception;

}
